package aaa.com.pp.activity;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.refreshlayout.BGANormalRefreshViewHolder;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;
import aaa.com.pp.R;
import aaa.com.pp.adapter.MultiRecyclerAdapter;
import aaa.com.pp.decorate.Visitable;
import aaa.com.pp.gsonbean.DataBean;
import aaa.com.pp.http.HttpMethods;
import aaa.com.pp.utils.Constant;
import rx.Subscriber;
import rx.exceptions.OnErrorFailedException;


public class MainActivity extends AppCompatActivity  implements BGARefreshLayout.BGARefreshLayoutDelegate {

    private android.support.v7.widget.RecyclerView rvContent;
    private cn.bingoogolapple.refreshlayout.BGARefreshLayout bgaRefreshLayout;

    MultiRecyclerAdapter mMultiRecyclerAdapter;
    List<Visitable> mVisitableList = new ArrayList<>();

    LinearLayoutManager llm;
    String arg = Constant.sCategoryList.get(1);//android标签
    int pagenum = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_gank);
        this.bgaRefreshLayout = (BGARefreshLayout) findViewById(R.id.rl_gank_refresh);
        this.rvContent = (RecyclerView) findViewById(R.id.rv_content);

        setBGALayout();
        initRefresh();
        initRvContent();
        initBGAData();

    }

    private void initRefresh() {
        bgaRefreshLayout.beginRefreshing();
        // android标签
        getContent(arg, 1);
    }


    private void setBGALayout() {
        // 为BGARefreshLayout 设置代理
        bgaRefreshLayout.setDelegate(this);
        // 设置下拉刷新和上拉加载更多的风格     参数1：应用程序上下文，参数2：是否具有上拉加载更多功能

        BGANormalRefreshViewHolder refreshViewHolder =
                new BGANormalRefreshViewHolder(this, true);
        refreshViewHolder.setLoadingMoreText(getString(R.string.load_more));
        refreshViewHolder.setLoadMoreBackgroundColorRes(R.color.white);
        refreshViewHolder.setRefreshViewBackgroundColorRes(R.color.white);
        bgaRefreshLayout.setRefreshViewHolder(refreshViewHolder);
    }

    private void initRvContent() {
        llm = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mMultiRecyclerAdapter = new MultiRecyclerAdapter(null);
        rvContent.setLayoutManager(llm);
        rvContent.setAdapter(mMultiRecyclerAdapter);
    }

    private void initBGAData() {

    }


    public void getContent(final String category, int pagenum) {
        Subscriber subscriber = new Subscriber<DataBean>() {
            @Override
            public void onCompleted() {
                if (bgaRefreshLayout.isLoadingMore()) {
                    bgaRefreshLayout.endLoadingMore();
                } else {
                    bgaRefreshLayout.endRefreshing();
                }
            }

            @Override
            public void onError(Throwable e) {
                try {
                    Snackbar.make(rvContent, "网络请求错误", Snackbar.LENGTH_SHORT).show();
                    e.printStackTrace();
                } catch (OnErrorFailedException errorFailedException) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onNext(DataBean httpBean) {
                if (bgaRefreshLayout.isLoadingMore()) {
                } else {
                    mVisitableList.clear();
                }
                mVisitableList.addAll(httpBean.getResults());
                mMultiRecyclerAdapter.setData(mVisitableList);
            }
        };
        HttpMethods.getInstance().getData(subscriber, category, "10", pagenum);
    }



    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        getContent(arg, 1);
    }
    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        getContent(arg, ++pagenum);
        return true;
    }
}
